﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Threading.Tasks;

namespace GPS.Controllers.GPS_CapPhep
{
    public class GPS_GiayPhepPhuongTienController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        public void initialCategorySearchAction(ViewModel_DLSPT viewModel)
        {
            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pPT_DVGTVT_MA);
            viewModel.pPT_DVGTVT_MA = getdvgtvt_Ma;
        }

        #region Index
        public ActionResult Index(ViewModel_DLSPT SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            List<CrudModel_DLSPT> model = new List<CrudModel_DLSPT>();
            if (SearchModel.SearchButton == "Search")
            {
                model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_GETLIST_DLSPT(null, SearchModel.pPT_BIENSO, SearchModel.pPT_SODK, SearchModel.pPT_DVGTVT_MA, SearchModel.pPT_ACTIVE);
            }
            ViewBag.TotalRow = model.Count();
            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }
        #endregion

        #region Detail
        public ActionResult _Detail(decimal? id1, string id2)
        {
            decimal? ID_PT = id1;
            string PT_DVGTVT_MA = id2;
            CrudModel_DLSPT model = new CrudModel_DLSPT();
            model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_GETITEM_DLSPT(ID_PT, "", "", PT_DVGTVT_MA, "");
            model.LSPT_BIENSO = model.PT_BIENSO;
            model.LSPT_DVGTVT_MA_CU = model.LSPT_DVGTVT_MA_CU == null ? model.PT_DVGTVT_MA : model.LSPT_DVGTVT_MA_CU;
            model.LSPT_NGAYGCNKD_CU = model.LSPT_NGAYGCNKD_CU == null ? model.PT_NGAYGCNKD : model.LSPT_NGAYGCNKD_CU;
            model.LSPT_NGAYHHGCNKD_CU = model.LSPT_NGAYHHGCNKD_CU == null ? model.PT_NGAYHHGCNKD : model.LSPT_NGAYHHGCNKD_CU;
            model.LSPT_SOGPXTL_CU = model.LSPT_SOGPXTL_CU == null ? model.PT_SOGPXTL : model.LSPT_SOGPXTL_CU;
            model.LSPT_CQCAPGPXTL_CU = model.LSPT_CQCAPGPXTL_CU == null ? model.PT_CQCAPGPXTL : model.LSPT_CQCAPGPXTL_CU;
            model.LSPT_NGAYGPXTL_CU = model.LSPT_NGAYGPXTL_CU == null ? model.PT_NGAYGPXTL : model.LSPT_NGAYGPXTL_CU;
            model.LSPT_NGAYHHGPXTL_CU = model.LSPT_NGAYHHGPXTL_CU == null ? model.PT_NGAYHHGPXTL : model.LSPT_NGAYHHGPXTL_CU;
            model.LSPT_STATUS = model.LSPT_STATUS == null ? model.PT_ACTIVE : model.LSPT_STATUS;
            return PartialView("_Detail", model);
        }
        #endregion

        public ActionResult _Create(decimal id1, string id2)
        {
            decimal? ID_PT = id1;
            string PT_DVGTVT_MA = id2;
            CrudModel_DLSPT model = new CrudModel_DLSPT();
            DPT _model = _db.Filter<DPT>(x => x.ID == ID_PT && x.PT_DVGTVT_MA == PT_DVGTVT_MA).FirstOrDefault();
            model.LSPT_BIENSO = _model.PT_BIENSO;
            model.LSPT_DVGTVT_MA_CU = _model.PT_DVGTVT_MA;
            model.LSPT_DVGTVT_MA_MOI = _model.PT_DVGTVT_MA;
            model.LSPT_NGAYGCNKD_CU = _model.PT_NGAYGCNKD;
            model.LSPT_NGAYHHGCNKD_CU = _model.PT_NGAYHHGCNKD;
            model.LSPT_SOGPXTL_CU = _model.PT_SOGPXTL;
            model.LSPT_CQCAPGPXTL_CU = _model.PT_CQCAPGPXTL;
            model.LSPT_NGAYGPXTL_CU = _model.PT_NGAYGPXTL;
            model.LSPT_NGAYHHGPXTL_CU = _model.PT_NGAYHHGPXTL;
            model.LSPT_STATUS = _model.PT_ACTIVE;
            return PartialView("_Create", model);
        }

        [HttpPost]
        public ActionResult _Create(CrudModel_DLSPT model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                model.LSPT_STATUS = "1";
                Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_DLSPT_INSERT(model);
                string url = Url.Action("Index", "GPS_GiayPhepPhuongTien");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("_Create", model);
            }
        }

        #region Cập nhật phép xe tập lái
        public ActionResult _Edit(decimal id1, string id2)
        {
            decimal _id = id1;
            string _GV_DVGTVT_MA = id2;
            decimal? ID_PT = id1;
            string PT_DVGTVT_MA = id2;
            CrudModel_DLSPT model = new CrudModel_DLSPT();
            model = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_GETITEM_DLSPT(ID_PT, "", "", PT_DVGTVT_MA, "");
            if (model != null)
            {
                model.LSPT_BIENSO = model.PT_BIENSO;
                model.LSPT_DVGTVT_MA_CU = model.LSPT_DVGTVT_MA_CU == null ? model.PT_DVGTVT_MA : model.LSPT_DVGTVT_MA_CU;
                model.LSPT_DVGTVT_MA_MOI = model.LSPT_DVGTVT_MA_MOI == null ? model.PT_DVGTVT_MA : model.LSPT_DVGTVT_MA_MOI;
                model.LSPT_NGAYGCNKD_CU = model.LSPT_NGAYGCNKD_CU == null ? model.PT_NGAYGCNKD : model.LSPT_NGAYGCNKD_CU;
                model.LSPT_NGAYHHGCNKD_CU = model.LSPT_NGAYHHGCNKD_CU == null ? model.PT_NGAYHHGCNKD : model.LSPT_NGAYHHGCNKD_CU;
                model.LSPT_SOGPXTL_CU = model.LSPT_SOGPXTL_CU == null ? model.PT_SOGPXTL : model.LSPT_SOGPXTL_CU;
                model.LSPT_CQCAPGPXTL_CU = model.LSPT_CQCAPGPXTL_CU == null ? model.PT_CQCAPGPXTL : model.LSPT_CQCAPGPXTL_CU;
                model.LSPT_NGAYGPXTL_CU = model.LSPT_NGAYGPXTL_CU == null ? model.PT_NGAYGPXTL : model.LSPT_NGAYGPXTL_CU;
                model.LSPT_NGAYHHGPXTL_CU = model.LSPT_NGAYHHGPXTL_CU == null ? model.PT_NGAYHHGPXTL : model.LSPT_NGAYHHGPXTL_CU;
                model.LSPT_STATUS = model.LSPT_STATUS == null ? model.PT_ACTIVE : model.LSPT_STATUS;
                return PartialView("_Edit", model);
            }
            else
            {
                ViewBag.MessengerAler = "";
                return PartialView("_Alert", model);
            }
        }

        [HttpPost]
        public ActionResult _Edit(CrudModel_DLSPT model, FormCollection form)
        {
            if (ModelState.IsValid)
            {
                CrudModel_DLSPT CheckExits = Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_DLSPT_CHECKEXITS(model.LSPT_DVGTVT_MA_CU, model.LSPT_DVGTVT_MA_MOI, model.LSPT_BIENSO);
                if (CheckExits == null)
                {
                    Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_DLSPT_INSERT(model);
                }
                else
                {
                    Singleton<GPS_DAL.GPS_CapPhepDAO>.Inst.GPS_CAPPHEP_DLSPT_UPDATE(model);
                }
                string url = Url.Action("Index", "GPS_GiayPhepPhuongTien");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("_Edit", model);
            }
        }

        #endregion

        #region xuất giấy
        public ActionResult Export(decimal _ID, string _CB_PT_BIENSO, string _CB_PT_DVGTVT_MA, string _CB_PT_DVGTVT_MA_QL, string _CB_PT_SOGPXTL, string _CB_PT_NGAYGPXTL, string _CB_PT_NGAYHHGPXTL)
        {
            try
            {
                DPT model = _db.GetOne<DPT>(x => x.ID == _ID && x.PT_DVGTVT_MA == _CB_PT_DVGTVT_MA);
                CrudModelDPT_Export _model = new CrudModelDPT_Export();
                _model.CB_PT_BIENSO = _CB_PT_BIENSO;
                _model.CB_PT_DVGTVT_MA = _CB_PT_DVGTVT_MA;
                _model.CB_PT_DVGTVT_MA_QL = _CB_PT_DVGTVT_MA_QL;
                _model.PT_NHANHIEU = model.PT_NHANHIEU;
                _model.PT_MAUXE = model.PT_MAUXE;
                _model.CB_PT_SOGPXTL = _CB_PT_SOGPXTL;
                _model.CB_PT_NGAYGPXTL = _CB_PT_NGAYGPXTL == null ? (DateTime?)null : DateTime.Parse(_CB_PT_NGAYGPXTL);
                _model.CB_PT_NGAYHHGPXTL = _CB_PT_NGAYHHGPXTL == null ? (DateTime?)null : DateTime.Parse(_CB_PT_NGAYHHGPXTL);
                _model.DATETIME_NOW = "Ngày " + DateTime.Now.Day.ToString() + " tháng " + DateTime.Now.Month.ToString() + " Năm " + DateTime.Now.Year.ToString();
                byte[] contentFile = null;
                contentFile = GPS.ENTITYS.EXPORTWORD_DPT_Export.EXPORT_DPT_Export(Server.MapPath("~/Templates/GPS_Word/GP_XeTapLai.docx"), _model, "DocxGenericReport");
                string nameOutput = string.Format("GiayCapPhepPhuongTien_{0}_{1}.{2}", _model.CB_PT_BIENSO, DateTime.Now.ToShortDateString().Replace("/", "_"), "docx");
                return File(contentFile, "application/vnd.openxmlformats-officedocument.wordprocessingml.document", nameOutput);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("ErrorMsg", ex.Message);
                return RedirectToAction("Index");
            }
        }

        #endregion
    }
}